package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;


/**
 * 只是操作代表的Service。可以 extends ServiceImpl<表Mapper, 表的Bean> implements 表Service
 */
@Slf4j
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {



        return baseAttrInfoMapper.getAttrInfoList(category1Id,category2Id,category3Id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveInfoAndValue(BaseAttrInfo baseAttrInfo) {
        //1、保存属性名
        baseAttrInfoMapper.insert(baseAttrInfo);
        //获取到刚才保留的记录的id
        Long id = baseAttrInfo.getId();

        //2、保存属性值
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if(!CollectionUtils.isEmpty(attrValueList)){
            for (BaseAttrValue baseAttrValue : attrValueList) {
                baseAttrValue.setAttrId(id);
                baseAttrValueMapper.insert(baseAttrValue);
            }
        }
    }

    /**
     * 修改平台属性名和值
     * @param baseAttrInfo
     */
    @Override
    public void updateInfoAndValue(BaseAttrInfo baseAttrInfo) {
        //属性名更新
        baseAttrInfoMapper.updateById(baseAttrInfo);

        //属性名id
        Long id = baseAttrInfo.getId();

        //属性值更新
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();

        //  以前：1,2
        //  现在：1,x,y,z
        // 把以前的全部删除
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",id);
        baseAttrValueMapper.delete(wrapper);

        //删除完成后新增所有
        for (BaseAttrValue baseAttrValue : attrValueList) {
              baseAttrValue.setAttrId(id);
              baseAttrValueMapper.insert(baseAttrValue);
//            if(baseAttrValue.getId() !=null){
//                //有属性值id是修改
//                baseAttrValueMapper.updateById(baseAttrValue);
//            }else {
//                //无属性值id是修改
//                baseAttrValue.setAttrId(id);
//                baseAttrValueMapper.insert(baseAttrValue);
//            }
        }


    }

    @Override
    public void saveOrUpdateInfoAndValue(BaseAttrInfo baseAttrInfo) {
        //新增
        if(baseAttrInfo.getId() == null){
            saveInfoAndValue(baseAttrInfo);
        }else{
            updateInfoAndValue(baseAttrInfo);
        }

        //修改数据：BaseAttrInfo{id='11'attrName='上市时间333', categoryId=1, categoryLevel=1, attrValueList=[BaseAttrValue{id='59'valueName='2020', attrId=11}, BaseAttrValue{id='null'valueName='dsadas', attrId=null}, BaseAttrValue{id='null'valueName='dsadas', attrId=null}]}

    }





//    @Autowired   //只要实现类继承了 extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>，就自动带上
//    BaseAttrInfoMapper baseAttrInfoMapper;



}
